package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbBrand;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbTypeTemplate;
import com.dongdong.service.BeandService;
import com.dongdong.service.SpecificationService;
import com.dongdong.service.TypeTemplateService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplateController")
public class TypeTemplateController {

    @Reference
    private BeandService beandService;

    @Reference
    private SpecificationService specificationService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @RequestMapping("list")
    public String list(){
        return "admin/type_template";
    }

    @RequestMapping("template")
    @ResponseBody
    public Result template(String name){
        List<TbTypeTemplate> templateList = typeTemplateService.selectAll(name);
        if(templateList == null || templateList.size()<=0){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(templateList);
    }

    @RequestMapping("pinpai")
    @ResponseBody
    public Result pinpai(){
        List<TbBrand> tbBrandList = beandService.selectAll();
        if(tbBrandList == null || tbBrandList.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbBrandList);
    }

    @RequestMapping("guige")
    @ResponseBody
    public Result guige(){
        List<TbSpecification> specificationList = specificationService.selectAll();
        if(specificationList == null || specificationList.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(specificationList);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbTypeTemplate tbTypeTemplate){
        boolean b = typeTemplateService.insert(tbTypeTemplate);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        boolean b = typeTemplateService.delete(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

            @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id){
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectId(id);
        if(tbTypeTemplate != null){
            return Result.builResult(ResultEnum.SUCCESS).setResout(tbTypeTemplate);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbTypeTemplate tbTypeTemplate){
        boolean b = typeTemplateService.update(tbTypeTemplate);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }
}
